import time
import hashlib
import requests
from datetime import datetime
import json
import os
##cron: 36 8 * * *
##new Env("吉利汽车")

# 从青龙环境变量获取配置（格式：TOKEN#DEVICESN）
env_data = os.getenv('jlqc_data')
if env_data and '#' in env_data:
    TOKEN, DEVICESN = env_data.split('#', 1)
else:
    TOKEN = ''
    DEVICESN = ''

# 固定信息
BASE_URL = "https://app.geely.com"
APP_VERSION = "3.30.0"
SECRET_KEY = "0]3K@'9MK+6Jf"  

# 初始化通知消息
notify_message = []

def get_headers():
    """生成请求头"""
    return {
        "User-Agent": "okhttp/4.9.3",
        "Content-Type": "application/json",
        "gl_dev_name": "PJA210",
        "gl_dev_model": "PJA210",
        "gl_dev_brand": "OPPO",
        "appversion": APP_VERSION,
        "gl_dev_id": DEVICESN,
        "gl_os_version": "35",
        "platform": "Android",
        "token": TOKEN,
        "gl_dev_platform": "android",
        "accept-language": "zh-Hans-CN;q=1",
        "devicesn": DEVICESN,
    }

def sign_in():
    """执行签到"""
    global notify_message
    if not TOKEN or not DEVICESN:
        msg = "❌ 签到失败：未配置TOKEN或DEVICESN（格式应为TOKEN#DEVICESN）"
        print(msg)
        notify_message.append(msg)
        return
    
    url = f"{BASE_URL}/api/v1/userSign/sign/risk"
    ts = int(time.time())
    sign_date = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    body = {
        "ts": ts,
        "cId": "BLqo2nmmoPgGuJtFDWlUjRI2b1b"
    }
    sign_str = f"cId=BLqo2nmmoPgGuJtFDWlUjRI2b1b&ts={ts}{SECRET_KEY}"
    md5_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    sweet_security_info = {
        "appVersion": APP_VERSION,
        "deviceUUID": DEVICESN,
        "geelyDeviceId": DEVICESN,
    }

    headers = get_headers()
    headers.update({
        "X-Data-Sign": md5_sign,
        "sweet_security_info": json.dumps(sweet_security_info)
    })

    print("签到请求发送中...")
    try:
        response = requests.post(url, headers=headers, json=body)
        response_data = response.json()
        if response_data.get("code") == "success":
            msg = f"✅ 签到成功：{response_data.get('message')}"
            print(msg)
            notify_message.append(msg)
        else:
            msg = f"❌ 签到失败：{response_data.get('message')}"
            print(msg)
            notify_message.append(msg)
    except Exception as e:
        msg = f"❌ 签到请求失败：{e}"
        print(msg)
        notify_message.append(msg)

def check_points():
    """查询积分"""
    global notify_message
    if not TOKEN or not DEVICESN:
        return
    
    url = f"{BASE_URL}/api/v1/point/available"
    headers = get_headers()

    print("积分查询请求发送中...")
    try:
        response = requests.get(url, headers=headers)
        response_data = response.json()
        if response_data.get("code") == "success":
            available_points = response_data.get("data", {}).get("availablePoint", 0)
            msg = f"📊 可用积分：{available_points}"
            print(msg)
            notify_message.append(msg)
        else:
            msg = f"❌ 查询积分失败：{response_data.get('message')}"
            print(msg)
            notify_message.append(msg)
    except Exception as e:
        msg = f"❌ 查询积分请求失败：{e}"
        print(msg)
        notify_message.append(msg)

def get_sign_info():
    """查询累计签到天数"""
    global notify_message
    if not TOKEN or not DEVICESN:
        return
    
    url = f"{BASE_URL}/api/v1/userSign/getSignMsg"
    headers = get_headers()
    current_date = datetime.now()
    body = {
        "year": current_date.year,
        "month": current_date.month
    }

    print("签到信息请求发送中...")
    try:
        response = requests.post(url, headers=headers, json=body)
        response_data = response.json()
        if response_data.get("code") == "success":
            continuous_days = response_data.get("data", {}).get("continuousSignDay", 0)
            msg = f"📅 已连续签到：{continuous_days} 天"
            print(msg)
            notify_message.append(msg)
        else:
            msg = f"❌ 查询累计签到失败：{response_data.get('message')}"
            print(msg)
            notify_message.append(msg)
    except Exception as e:
        msg = f"❌ 查询累计签到请求失败：{e}"
        print(msg)
        notify_message.append(msg)

def send_notification():
    """发送通知"""
    if not notify_message:
        return
    
    try:
        from notify import send as notify_send
        title = "吉利汽车签到结果"
        content = "\n".join(notify_message)
        notify_send(title, content)
        print("通知发送成功")
    except ImportError:
        print("未找到通知模块，跳过通知发送")
    except Exception as e:
        print(f"通知发送失败：{e}")

if __name__ == "__main__":
    sign_in()
    get_sign_info()
    check_points()
    send_notification()